﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;
namespace Ecreate.Web.systemanage.customer
{
    public partial class customer_insert : System.Web.UI.Page
    {
        protected string areaname = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string areacode = this.Request.QueryString["areacode"];

                hid_areacode.Value = areacode;

                string provincecode = areacode.Substring(0, 2);
                string citycode = areacode.Substring(0, 4);
                this.setDropDownList(provincecode, citycode, areacode);

                setDropDownList();
                List<string> l = new List<string>();
                l.Add("你父亲的名字？");
                l.Add("你小学老师的名字？");
                l.Add("你父亲的名字？");
                this.ddl_question.DataSource = l;
                this.ddl_question.DataBind();
            }
            else
            {
                        CustomerService service = new CustomerService();

                        if (this.ddl_province.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择省份";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.ddl_city.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择城市";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.ddl_country.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择县";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        string id = this.txt_cus_id.Text;

                        if (id == "")
                        {

                            this.lbl_message.Text = "请输入用户ID";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        else
                        {

                            t_customer search_customer = service.getCustomerByID(id);
                            if (search_customer != null)
                            {
                                this.lbl_message.Text = "用户ID重复,请重新输入!";
                                this.lbl_message.Visible = true;
                                return;
                            }
                        }
                        if (this.txt_cus_name.Text == "")
                        {
                            this.lbl_message.Text = "请输入用户名称";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_address.Text == "")
                        {
                            this.lbl_message.Text = "请输入销售商地址";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_tel.Text == "")
                        {
                            this.lbl_message.Text = "请输入销售商电话";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_linkman.Text == "")
                        {
                            this.lbl_message.Text = "请输入联系人";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_password.Text == "")
                        {
                            this.lbl_message.Text = "请输入密码";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_password.Text != this.txt_confirmpassword.Text)
                        {
                            this.lbl_message.Text = "两次输入的密码不致!";
                            this.lbl_message.Visible = true;
                            return;


                        }
                        if (this.txt_answer.Text == "")
                        {

                            this.lbl_message.Text = "请找回密码答案";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        t_customer cus = new t_customer();
                        cus.Cus_id = this.txt_cus_id.Text;
                        cus.Cus_name = this.Request.Form["txt_cus_name"];
                        cus.Cus_address = this.Request.Form["txt_cus_address"];
                        cus.Cus_linkman = this.Request.Form["txt_cus_linkman"];
                        cus.Cus_tel = this.Request.Form["txt_cus_tel"];
                        cus.Areacode = this.ddl_country.SelectedValue.ToString();
                        cus.Password = this.txt_password.Text;
                        cus.Passwordquestion = ddl_question.Text;
                        cus.Passwordanswer = this.txt_answer.Text;
                        service.Insert(cus);
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">          alert('数据保存成功!')　　; </script>");
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"client\"].location=\"/systemanage/customer/customerlist.aspx?areacode="+this.hid_areacode.Value+"\"　　; </script>");

            }
        }

        public void setDropDownList(string provincecode, string citycode, string countrycode)
        {



            AreaService aservice = new AreaService();

            if (provincecode == "")
            {
                this.ddl_province.DataSource = aservice.getProvinceArea();
            }
            else
            {
                this.ddl_province.DataSource = aservice.getProvinceArea(provincecode);
            }
            this.ddl_province.DataValueField = "areacode";
            this.ddl_province.DataTextField = "areaname";
            this.ddl_province.DataBind();

            if (ddl_province.SelectedValue != null)
            {
                string id = ddl_province.SelectedValue.ToString();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();
                if (citycode != "")
                {
                    ddl_city.SelectedValue = citycode;
                }


            }
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
                if (countrycode != "")
                {

                    this.ddl_country.SelectedValue = countrycode;
                }
            }

        }

        public void setDropDownList()
        {



            AreaService aservice = new AreaService();
            this.ddl_province.DataSource = aservice.getProvinceArea();
            this.ddl_province.DataValueField = "areacode";
            this.ddl_province.DataTextField = "areaname";
            this.ddl_province.DataBind();

            if (ddl_province.SelectedValue != null)
            {
                string id = ddl_province.SelectedValue.ToString();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();
            }
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
            }

        }


        protected void ddl_province_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_province.SelectedValue!=null)
            {
                string id = ddl_province.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();


            }
        }

        protected void ddl_city_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
            }
        }
    }
}
